package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    //添加预约数据方法
    @Override
        public void add(List<OrderSetting> list){
        //有集合先判断是否为空
            if (list != null && list.size() > 0) {
                for (OrderSetting orderSetting : list) {
                    //检查此数据（日期）是否存在
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                //已经存在，执行更新操作
                if (count > 0) {
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                    //不存在，执行添加操作
                } else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }
    //显示预约数据方法
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String dateBegin = date + "-1";
        String dateEnd = date + "-31";
        //可以不适用map  在接口使用@Param注解
        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        //获取预约数据
        //把预约数据转换成需要的类型  存入到map集合
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());
            //获得日期 （几号）
            orderSettingMap.put("number",orderSetting.getNumber());
            //可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());
            //已预约人 数
            data.add(orderSettingMap);
        }return data;
    }
    //修改预约数据方法
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断之前有没有信息
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        //如果有修改之前数据
        if (count>0){
            orderSettingDao.editNumberByOrderDate(orderSetting);
            //如果没有添加该数据
        }else {
            orderSettingDao.add(orderSetting);
        }
    }
}


